package com.chainyoung.component_tmperson.ui.mvp.presenter;

import com.chainyoung.common.appcommon.entity.CommonList;
import com.chainyoung.common.appcommon.entity.UploadFile;
import com.chainyoung.common.base.CommonSubscriber;
import com.chainyoung.common.di.scope.ActivityScope;
import com.chainyoung.common.exception.ApiException;
import com.chainyoung.common.mvp.BasePresenter;
import com.chainyoung.common.utils.RxUtils;
import com.chainyoung.common.utils.UpLoadUtils;
import com.chainyoung.component_tmperson.entity.Bank;
import com.chainyoung.component_tmperson.entity.Purse;
import com.chainyoung.component_tmperson.ui.mvp.contract.BindRecieveAccountContract;
import com.chainyoung.component_tmperson.ui.mvp.contract.WalletContact;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import javax.inject.Inject;

import okhttp3.RequestBody;

import static com.chainyoung.common.constant.LibConstant.NO_RESULT_ERRORCODE;

@ActivityScope
public class BindRecieveAccountPresenter extends BasePresenter<BindRecieveAccountContract.Model, BindRecieveAccountContract.View> {


    @Inject
    public BindRecieveAccountPresenter(BindRecieveAccountContract.Model model, BindRecieveAccountContract.View rootView) {
        super(model, rootView);
    }


    public void setRecieveAccount(TreeMap<String, Object> map) {
        addSubscribe(mModel.setReceiveAccount(map)
                .compose(RxUtils.<String>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<String>(mView,true) {
                    @Override
                    public void onNext(String data) {
                        mView.onSetRecieveAccountSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));
    }

    public void getBanks() {
        addSubscribe(mModel.getBanks()
                .compose(RxUtils.<List<Bank>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<List<Bank>>(mView) {
                    @Override
                    public void onNext(List<Bank> data) {
                        mView.onGetBanks(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException){
                            if (((ApiException) e).getErrorCode().equals(NO_RESULT_ERRORCODE)){
                                mView.onGetBanks(new ArrayList<Bank>());
                                return;
                            }
                        }
                        super.onError(e);
                    }
                }));
    }

    public void upLoadCodeImg(String path,int type) {
        //Map<String, RequestBody> map=UpLoadUtils.getBody(path,type);
        RequestBody body= UpLoadUtils.getBody(path,type);
        addSubscribe(mModel.upLoadCodeImg(body)
                .compose(RxUtils.<UploadFile>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<UploadFile>(mView,true) {
                    @Override
                    public void onNext(UploadFile data) {
                        mView.onUpLoadCodeSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));
    }


}

